<?php

namespace ai\response;

use  Yii;
use yii\helpers\Url;

class Response
{
    function susess($data = [], $msg = '')
    {
        self::response(true, $msg, $data);
    }

    function error($msg = '', $data = [])
    {
        self::response(false, $msg, $data);
    }

    function paging($data, $msg = '')
    {
        $data['code'] = !count($data['data']) ? -1 : (empty($msg) ? 0 : -1);
        $data['msg'] = !count($data['data']) ? '暂无相关数据' : $msg;

        echo json_encode($data);
        exit;
    }

    function render($context, $view, $data = [])
    {
        return $context->render($view, $data);
    }

    function redirect($route, $param = '')
    {
        $res = Yii::$app->getResponse()->redirect(Url::to($route) . $param, 302);
        $res->send();
        exit;
        //$context->redirect($route)->send();
    }

    private static function response($status, $msg, $data)
    {
        $res = ['flag' => $status, 'code' => $status ? 0 : -1, 'msg' => $msg, 'data' => $data];
        echo json_encode($res);
        exit;
    }
}